package com.leador.map.api.location;

import android.location.Location;
import android.os.Bundle;

/**
 * 位置监听器代理
 */
public class LocationListenerProxy implements MapLocationListener {
	private LocationManagerProxy mLocationManager;
	private MapLocationListener mListener = null;

	public LocationListenerProxy(final LocationManagerProxy pLocationManager) {
		mLocationManager = pLocationManager;
	}

	public boolean startListening(final MapLocationListener pListener,
			final long pUpdateTime, final float pUpdateDistance,
			final String provider) {
		boolean result = false;
		mListener = pListener;
		if (LocationProviderProxy.LocNetwork.equals(provider)) {
			result = true;
			mLocationManager.requestLocationUpdates(provider, pUpdateTime,
					pUpdateDistance, this);
		}
		return result;
	}

	public boolean startListening(final MapLocationListener pListener,
			final long pUpdateTime, final float pUpdateDistance) {
		boolean result = false;
		mListener = pListener;
		for (final String provider : mLocationManager.getProviders(true)) {
			if (LocationManagerProxy.GPS_PROVIDER.equals(provider)
					|| LocationManagerProxy.NETWORK_PROVIDER.equals(provider)) {
				result = true;
				mLocationManager.requestLocationUpdates(provider, pUpdateTime,
						pUpdateDistance, this);
			}
		}
		return result;
	}

	public void stopListening() {
		if (mLocationManager != null) {
			mLocationManager.removeUpdates(this);
		}
		mListener = null;
	}

	@Override
	public void onLocationChanged(Location location) {
		if (mListener != null) {
			mListener.onLocationChanged(location);
		}
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		if (mListener != null) {
			mListener.onStatusChanged(provider, status, extras);
		}
	}

	@Override
	public void onProviderEnabled(String provider) {
		if (mListener != null) {
			mListener.onProviderEnabled(provider);
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		if (mListener != null) {
			mListener.onProviderDisabled(provider);
		}
	}

	@Override
	public void onLocationChanged(MapLocation location) {
		if (mListener != null) {
			mListener.onLocationChanged(location);
		}
	}
}
